$(document).ready(function (){
	view_init();
	event_init();
});

var url_param_id = getQueryString("id");

function view_init()
{
	loadingStart();
//	$("[data-type=dateAll]").each(function (){
//		$(this).datetimepicker({
//			  timeFormat: "HH:mm:ss",
//	          dateFormat: "yy-mm-dd",
//			  monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]
//		});
//	});
 	if (url_param_id)
 	{
 		um_ajax_post(
 				"/libmanagement/select.do",
 				{"id":url_param_id},
 				function (data){
 					if (data && data.libManagement)
 					{
 	 					$("#main-content").umDataBind("render" ,data.libManagement[0] ,"");
 	 					$("#libName").next().show();
 	 					$("#libName").next().attr("src" ,data.libManagement[0].libName);
 	 					$("#libCode").next().show();
 	 					$("#libCode").next().attr("src" ,data.libManagement[0].libCode);
 	 					$("#pisition").next().show();
 	 					$("#pisition").next().attr("src" ,data.libManagement[0].pisition);
 	 					$("#phone").next().show();
 	 					$("#phone").next().attr("src" ,data.libManagement[0].phone);
 	 					$("#lon").next().show();
 	 					$("#lon").next().attr("src" ,data.libManagement[0].lon);
 	 					$("#lat").next().show();
 	 					$("#lat").next().attr("src" ,data.libManagement[0].lat);
// 	 					var lon_title=document.getElementById("lon_title").value;
// 	 					var lat_title=document.getElementById("lat_title").value;
 					}

 				},
 				null,false);
 	}
 	
	loadingEnd();
	$("#main-content").show();
}

function event_init()
{
    $("#edit").click(function (){
    	childrenbooks_edit();
    });
    
    
    $("#generator").click(function (){
    	
    	
//    	alert(document.getElementById("lon_buf").value);
//    	alert(document.getElementById("lat_buf").value);
    	
    	document.getElementById("lon_title").value=document.getElementById("lon_buf").value;
    	document.getElementById("lat_title").value=document.getElementById("lat_buf").value;

//    	alert(document.getElementById("position_buf").value);
    });
    
	// 返回按钮
	$("#back").click(function (){
		window.location.href = "/libmanagement/direction/index.do";
	});
	
	$("#homePageChk").click(function (){
		if ($(this).is(":checked"))
		{
			$("#homePageDiv").show();
		}
		else
		{
			$("#homePageDiv").hide();
		}
	});
}

function childrenbooks_edit()
{
	if (!$("#main-content").validate())
	{
		return false;
	}
	
	var obj = $("#main-content").umDataBind("serialize" ,"");
	var url = "/libmanagement/add.do";

	if (url_param_id)
	{
		url = "/libmanagement/update.do";
		obj.id = url_param_id;
	}
	
	if ($("#homePageChk").is(":checked"))
	{
		obj.isHomePage = 1;
	}
	obj.bookType = 2;//普通图书
	um_ajax_post(
			url,
			obj,
			function (data){
				window.location.href = "/libmanagement/direction/index.do?flag=" + SUCCESS;
			},
			null);
}